home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / interface / OptionsDlg.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  11KB  |  415 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. // Options.cpp: Implementierungsdatei
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "../version.h"
  24. #include "filezilla server.h"
  25. #include "OptionsDlg.h"
  26. #include "..\OptionTypes.h"
  27. #include "Options.h"
  28.  
  29. #if defined(_DEBUG) && !defined(MMGR)
  30. #define new DEBUG_NEW
  31. #undef THIS_FILE
  32. static char THIS_FILE[] = __FILE__;
  33. #endif
  34.  
  35. #include "OptionsPage.h"
  36. #include "OptionsGeneralPage.h"
  37. #include "OptionsGeneralWelcomemessagePage.h"
  38. #include "OptionsPasvPage.h"
  39. #include "OptionsSecurityPage.h"
  40. #include "OptionsMiscPage.h"
  41. #include "OptionsAdminInterfacePage.h"
  42. #include "OptionsLoggingPage.h"
  43. #include "OptionsGSSPage.h"
  44. #include "OptionsSpeedLimitPage.h"
  45. #include "OptionsCompressionPage.h"
  46. #include "OptionsGeneralIpbindingsPage.h"
  47. #include "OptionsIpFilterPage.h"
  48. #include "OptionsSslPage.h"
  49. #include "OptionsAutobanPage.h"
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // Dialogfeld COptionsDlg 
  53.  
  54. COptionsDlg::COptionsDlg(COptions *pInterfaceOptions, bool localConnection)
  55.     : m_localConnection(localConnection)
  56. {
  57.     ASSERT(pInterfaceOptions);
  58.     m_pInterfaceOptions = pInterfaceOptions;
  59.  
  60.     //Add options pages
  61.     COptionsPage *page;
  62.  
  63.     page = new COptionsGeneralPage(this);
  64.     AddPage(*page, IDS_OPTIONSPAGE_GENERAL);
  65.     m_PageList.push_back(page);
  66.  
  67.         page = new COptionsGeneralWelcomemessagePage(this);
  68.         AddPage(*page, IDS_OPTIONSPAGE_GENERAL_WELCOMEMESSAGE, m_PageList.back());
  69.         m_PageList.push_back(page);
  70.  
  71.         page = new COptionsGeneralIpbindingsPage(this);
  72.         AddPage(*page, IDS_OPTIONSPAGE_GENERAL_IPBINDINGS, *--(--m_PageList.end()));
  73.         m_PageList.push_back(page);
  74.  
  75.         page = new COptionsIpFilterPage(this);
  76.         AddPage(*page, IDS_OPTIONSPAGE_GENERAL_IPFILTER, m_PageList.front());
  77.         m_PageList.push_back(page);
  78.  
  79.     page = new COptionsPasvPage(this);
  80.     AddPage(*page, IDS_OPTIONSPAGE_PASV);
  81.     m_PageList.push_back(page);
  82.  
  83.     page = new COptionsSecurityPage(this);
  84.     AddPage(*page, IDS_OPTIONSPAGE_SECURITY);
  85.     m_PageList.push_back(page);
  86.  
  87.     page = new COptionsMiscPage(this);
  88.     AddPage(*page, IDS_OPTIONSPAGE_MISC);
  89.     m_PageList.push_back(page);
  90.  
  91.     page = new COptionsAdminInterfacePage(this);
  92.     AddPage(*page, IDS_OPTIONSPAGE_ADMININTERFACE);
  93.     m_PageList.push_back(page);
  94.  
  95.     page = new COptionsLoggingPage(this);
  96.     AddPage(*page, IDS_OPTIONSPAGE_LOGGING);
  97.     m_PageList.push_back(page);
  98.  
  99.     page = new COptionsGSSPage(this);
  100.     AddPage(*page, IDS_OPTIONSPAGE_GSS);
  101.     m_PageList.push_back(page);
  102.  
  103.     m_pOptionsSpeedLimitPage = new COptionsSpeedLimitPage(this);
  104.     AddPage(*m_pOptionsSpeedLimitPage, IDS_OPTIONSPAGE_SPEEDLIMIT);
  105.     m_PageList.push_back(m_pOptionsSpeedLimitPage);
  106.  
  107.     page = new COptionsCompressionPage(this);
  108.     AddPage(*page, IDS_OPTIONSPAGE_COMPRESSION);
  109.     m_PageList.push_back(page);
  110.  
  111.     page = new COptionsSslPage(this);
  112.     AddPage(*page, IDS_OPTIONSPAGE_SSL);
  113.     m_PageList.push_back(page);
  114.  
  115.     page = new COptionsAutobanPage(this);
  116.     AddPage(*page, IDS_OPTIONSPAGE_AUTOBAN);
  117.     m_PageList.push_back(page);
  118. }
  119.  
  120. COptionsDlg::~COptionsDlg()
  121. {
  122.     for (std::list<COptionsPage *>::iterator iter = m_PageList.begin(); iter != m_PageList.end(); iter++)
  123.         delete *iter;
  124. }
  125.  
  126. void COptionsDlg::DoDataExchange(CDataExchange* pDX)
  127. {
  128.     CSAPrefsDialog::DoDataExchange(pDX);
  129.     //{{AFX_DATA_MAP(COptionsDlg)
  130.         // HINWEIS: Der Klassen-Assistent fⁿgt hier DDX- und DDV-Aufrufe ein
  131.     //}}AFX_DATA_MAP
  132. }
  133.  
  134. BEGIN_MESSAGE_MAP(COptionsDlg, CSAPrefsDialog)
  135.     //{{AFX_MSG_MAP(COptionsDlg)
  136.     //}}AFX_MSG_MAP
  137. END_MESSAGE_MAP()
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140. // Behandlungsroutinen fⁿr Nachrichten COptionsDlg 
  141.  
  142. BOOL COptionsDlg::Show()
  143. {
  144.     SetConstantText("FileZilla Server");
  145.  
  146.     std::list<COptionsPage *>::iterator iter;
  147.     for (iter = m_PageList.begin(); iter != m_PageList.end(); iter++)
  148.         (*iter)->LoadData();
  149.  
  150.     if (DoModal()!=IDOK)
  151.         return FALSE;
  152.  
  153.     for (iter = m_PageList.begin(); iter != m_PageList.end(); iter++)
  154.         (*iter)->SaveData();
  155.  
  156.     return TRUE;
  157. }
  158.  
  159. BOOL COptionsDlg::OnInitDialog() 
  160. {
  161.     CSAPrefsDialog::OnInitDialog();
  162.         
  163.     return TRUE;  // return TRUE unless you set the focus to a control
  164.                   // EXCEPTION: OCX-Eigenschaftenseiten sollten FALSE zurⁿckgeben
  165. }
  166.  
  167. void COptionsDlg::SetOption(int nOptionID, __int64 value)
  168. {
  169.     m_OptionsCache[nOptionID-1].nType = 1;
  170.     m_OptionsCache[nOptionID-1].value = value;
  171. }
  172.  
  173. void COptionsDlg::SetOption(int nOptionID, CString value)
  174. {
  175.     m_OptionsCache[nOptionID-1].nType = 0;
  176.     m_OptionsCache[nOptionID-1].str = value;
  177. }
  178.  
  179. CString COptionsDlg::GetOption(int nOptionID)
  180. {
  181.     ASSERT(nOptionID>0 && nOptionID<=OPTIONS_NUM);
  182.     ASSERT(!m_Options[nOptionID-1].nType);
  183.  
  184.     return m_OptionsCache[nOptionID-1].str;
  185. }
  186.  
  187. __int64 COptionsDlg::GetOptionVal(int nOptionID)
  188. {
  189.     ASSERT(nOptionID>0 && nOptionID<=OPTIONS_NUM);
  190.     ASSERT(m_Options[nOptionID-1].nType == 1);
  191.     
  192.     return m_OptionsCache[nOptionID-1].value;
  193. }
  194.  
  195. void COptionsDlg::OnOK() 
  196. {
  197.     if (!UpdateData(true))
  198.         return;
  199.     if (!GetCurPage()->UpdateData(TRUE))
  200.         return;
  201.  
  202.     for (std::list<COptionsPage *>::iterator iter = m_PageList.begin(); iter != m_PageList.end(); iter++)
  203.         if (!(*iter)->IsDataValid())
  204.             return;
  205.         
  206.     CSAPrefsDialog::OnOK();
  207. }
  208.  
  209. CSAPrefsSubDlg* COptionsDlg::GetCurPage()
  210. {
  211.     int iPage = m_iCurPage;
  212.     // show the new one
  213.     if ((iPage >= 0) && (iPage < (int)m_pages.size()))
  214.     {
  215.         pageStruct *pPS = m_pages[iPage];
  216.         ASSERT(pPS);
  217.  
  218.         if (pPS)
  219.         {
  220.             ASSERT(pPS->pDlg);
  221.             if (pPS->pDlg)
  222.             {
  223.                 return pPS->pDlg;
  224.             }
  225.         }
  226.     }
  227.     return NULL;
  228. }
  229.  
  230. bool COptionsDlg::AddPage(CSAPrefsSubDlg &page, UINT nCaptionID, CSAPrefsSubDlg *pDlgParent /*= NULL*/)
  231. {
  232.     CString str;
  233.     str.LoadString(nCaptionID);
  234.     return CSAPrefsDialog::AddPage(page, str, pDlgParent);
  235. }
  236.  
  237. bool COptionsDlg::IsNumeric(LPCTSTR str)
  238. {
  239.     if (!str)
  240.         return false;
  241.     LPCTSTR p=str;
  242.     while(*p)
  243.     {
  244.         if (*p<'0' || *p>'9')
  245.         {
  246.             return false;
  247.         }
  248.         p++;
  249.     }
  250.     return true;
  251. }
  252.  
  253. BOOL COptionsDlg::Init(unsigned char *pData, DWORD dwDataLength)
  254. {
  255.     unsigned char *p = pData;
  256.     int i;
  257.     int num = *p * 256 + p[1];
  258.     p+=2;
  259.     if (num!=OPTIONS_NUM)
  260.         return FALSE;
  261.     
  262.     for (i=0; i<num; i++)
  263.     {
  264.         if (static_cast<DWORD>(p-pData) >= dwDataLength)
  265.             return FALSE;
  266.         int nType = *p++;
  267.         if (!nType)
  268.         {
  269.             if (static_cast<DWORD>(p-pData+3) >= dwDataLength)
  270.                 return 2;
  271.             int len= *p * 256 * 256 + p[1] * 256 + p[2];
  272.             p+=3;
  273.             if (static_cast<DWORD>(p-pData+len)>dwDataLength)
  274.                 return FALSE;
  275.             m_OptionsCache[i].nType = 0;
  276.  
  277.             char* tmp = new char[len + 1];
  278.             memcpy(tmp, p, len);
  279.             tmp[len] = 0;
  280. #if _UNICODE
  281.             m_OptionsCache[i].str = ConvFromNetwork(tmp);
  282. #else
  283.             m_OptionsCache[i].str = ConvToLocal(ConvFromNetwork(tmp));
  284. #endif
  285.             p += len;
  286.  
  287.             delete [] tmp;
  288.         }
  289.         else if (nType == 1)
  290.         {
  291.             if (static_cast<DWORD>(p-pData+8)>dwDataLength)
  292.                 return FALSE;
  293.             m_OptionsCache[i].nType = 1;
  294.             memcpy(&m_OptionsCache[i].value, p, 8);
  295.             p+=8;
  296.         }
  297.         else
  298.             return FALSE;
  299.     }
  300.  
  301.     if (static_cast<DWORD>(p-pData+2) > dwDataLength)
  302.         return FALSE;
  303.     num = *p++ << 8;
  304.     num |= *p++;
  305.     for (i=0; i<num; i++)
  306.     {
  307.         CSpeedLimit limit;
  308.         p = limit.ParseBuffer(p, dwDataLength - (p - pData));
  309.         if (!p)
  310.             return FALSE;
  311.         m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits.push_back(limit);
  312.     }
  313.     
  314.     if (static_cast<DWORD>(p-pData+2) > dwDataLength)
  315.         return FALSE;
  316.     num = *p++ << 8;
  317.     num |= *p++;
  318.     for (i=0; i<num; i++)
  319.     {
  320.         CSpeedLimit limit;
  321.         p = limit.ParseBuffer(p, dwDataLength - (p - pData));
  322.         if (!p)
  323.             return FALSE;
  324.         m_pOptionsSpeedLimitPage->m_UploadSpeedLimits.push_back(limit);
  325.     }
  326.     return TRUE;
  327. }
  328.  
  329. BOOL COptionsDlg::GetAsCommand(char **pBuffer, DWORD *nBufferLength)
  330. {
  331.     DWORD len = 2;
  332.     int i;
  333.     for (i = 0; i < OPTIONS_NUM; i++)
  334.     {
  335.         len+=1;
  336.         if (!m_Options[i].nType)
  337.         {
  338.             char* utf8 = ConvToNetwork(GetOption(i+1));
  339.             if (utf8)
  340.             {
  341.                 int l = strlen(utf8);
  342.                 delete [] utf8;
  343.  
  344.                 if (l > 0xFFFFFF)
  345.                     return FALSE;
  346.  
  347.                 len += l;
  348.             }
  349.             len += 3;
  350.         }
  351.         else
  352.             len+=8;
  353.     }
  354.     len += 4; //Number of rules
  355.     SPEEDLIMITSLIST::const_iterator iter;
  356.     for (iter = m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits.begin(); iter != m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits.end(); iter++)
  357.         len += iter->GetRequiredBufferLen();
  358.     for (iter = m_pOptionsSpeedLimitPage->m_UploadSpeedLimits.begin(); iter != m_pOptionsSpeedLimitPage->m_UploadSpeedLimits.end(); iter++)
  359.         len += iter->GetRequiredBufferLen();
  360.  
  361.     *pBuffer = new char[len];
  362.     char *p=*pBuffer;
  363.     *p++ = OPTIONS_NUM/256;
  364.     *p++ = OPTIONS_NUM%256;
  365.     for (i=0; i<OPTIONS_NUM; i++)
  366.     {
  367.         *p++ = m_Options[i].nType;
  368.         switch(m_Options[i].nType) {
  369.         case 0:
  370.             {
  371.                 char* utf8 = ConvToNetwork(GetOption(i+1));
  372.                 if (utf8)
  373.                 {
  374.                     int slen = strlen(utf8);
  375.                     *p++ = (slen / 256) / 256;
  376.                     *p++ = slen / 256;
  377.                     *p++ = slen % 256;
  378.                     memcpy(p, utf8, slen);
  379.                     p += slen;
  380.                     delete [] utf8;
  381.                 }
  382.                 else
  383.                 {
  384.                     *p++ = 0;
  385.                     *p++ = 0;
  386.                 }
  387.             }
  388.             break;
  389.         case 1:
  390.             {
  391.                 _int64 value = GetOptionVal(i+1);
  392.                 memcpy(p, &value, 8);
  393.                 p+=8;
  394.             }
  395.             break;
  396.         default:
  397.             ASSERT(FALSE);
  398.         }
  399.     }
  400.  
  401.     *p++ = m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits.size() >> 8;
  402.     *p++ = m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits.size() % 256;
  403.     for (iter = m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits.begin(); iter != m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits.end(); iter++)
  404.         p = iter->FillBuffer(p);
  405.     
  406.     *p++ = m_pOptionsSpeedLimitPage->m_UploadSpeedLimits.size() >> 8;
  407.     *p++ = m_pOptionsSpeedLimitPage->m_UploadSpeedLimits.size() % 256;
  408.     for (iter = m_pOptionsSpeedLimitPage->m_UploadSpeedLimits.begin(); iter != m_pOptionsSpeedLimitPage->m_UploadSpeedLimits.end(); iter++)
  409.         p = iter->FillBuffer(p);
  410.  
  411.     *nBufferLength = len;
  412.  
  413.     return TRUE;
  414. }
  415.